using System;
using System.Collections.Generic;
using System.Linq;

using Csla;

using EntModApp.EntServices.Business;
using EntModApp.EntServices.IoC;

using DemoApp.DataAccess;

namespace DemoApp.BusinessLayer 
{
  [Serializable]
  public partial class CslaRoleEdit : 
    BusinessBase<CslaRoleEdit>,
    IRole
  {
    #region [ Business Properties ]

    public static PropertyInfo<Guid> RoleIdPropertyInfo = RegisterProperty(new PropertyInfo<Guid>("RoleId"));
    public Guid RoleId 
    { 
      get
      {
        return GetProperty(RoleIdPropertyInfo);
      }
      private set
      {
        LoadProperty(RoleIdPropertyInfo, value);
      }
    }

    public static PropertyInfo<string> NamePropertyInfo = RegisterProperty(new PropertyInfo<string>("Name"));
    public string Name 
    { 
      get
      {
        return GetProperty(NamePropertyInfo);
      }
      set
      {
        LoadProperty(NamePropertyInfo, value);
      }
    }

    public static PropertyInfo<bool> IsAdminPropertyInfo = RegisterProperty(new PropertyInfo<bool>("IsAdmin"));
    public bool IsAdmin 
    { 
      get
      {
        return GetProperty(IsAdminPropertyInfo);
      }
      set
      {
        LoadProperty(IsAdminPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaSecurableActionEditCollection> SecurableActionsPropertyInfo = RegisterProperty(new PropertyInfo<CslaSecurableActionEditCollection>("SecurableActions"));
    public CslaSecurableActionEditCollection SecurableActions 
    { 
      get
      {
        return GetProperty(SecurableActionsPropertyInfo);
      }
      private set
      {
        LoadProperty(SecurableActionsPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaUserEditCollection> UsersPropertyInfo = RegisterProperty(new PropertyInfo<CslaUserEditCollection>("Users"));
    public CslaUserEditCollection Users 
    { 
      get
      {
        return GetProperty(UsersPropertyInfo);
      }
      private set
      {
        LoadProperty(UsersPropertyInfo, value);
      }
    }

    #endregion [ Business Properties ]

    #region [ Validation Rules ]
    #endregion [ Validation Rules ]

    #region [ Authorization Rules ]
    #endregion [ Authorization Rules ]
	
    #region [ Factory Methods ]

    #region [ Root Factory Methods ]
    public static CslaRoleEdit New()
    {
      return DataPortal.Create<CslaRoleEdit>();
    }

    public static void Delete(RoleKey key)
    {
      DataPortal.Delete<Role>(new SingleCriteria<Role, RoleKey>(key));
    }

    public static void Delete(Guid RoleId)
    {
      Delete(new RoleKey { RoleId = RoleId });
    }
    #endregion [ Root Factory Methods ]

    #region [ Child Factory Methods ]
    internal static CslaRoleEdit NewChild()
    {
      return DataPortal.CreateChild<CslaRoleEdit>();
    }

    internal static CslaRoleEdit GetRoleEdit(Role data, RoleLoadSettings loadSettings)
    {
      if (data == null) throw new ArgumentNullException("data");
      if (loadSettings == null) throw new ArgumentNullException("loadSettings");
      CslaRoleEdit result = new CslaRoleEdit();
      result.FetchRole(data, loadSettings);
      result.MarkAsChild();
      return result;
    }
    #endregion [ Child Factory Methods ]

    private static int ForceInitField = 0;
    protected CslaRoleEdit() 
    {
      ForceInitField = 1;
    }
    #endregion [ Factory Methods ]

    #region [ Data Access ]

    #region [ Root Data Access ]
    protected override void DataPortal_Create()
    {
      base.DataPortal_Create();
    }

    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Insert()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Update()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_DeleteSelf()
    {
      DataPortal_Delete(new SingleCriteria<Role, RoleKey>(new RoleKey { RoleId = this.RoleId }));
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    private void DataPortal_Delete(SingleCriteria<Role, RoleKey> criteria)
    {
    }
    #endregion [ Root Data Access ]

    #region [ Child Data Access ]
    protected override void Child_Create()
    {
      base.Child_Create();
    }
    
    private void Child_Fetch(object childData)
    {
    }
    
    private void Child_Insert(object parent)
    {
    }
    
    private void Child_Update(object parent)
    {
    }
    
    private void Child_DeleteSelf(object parent)
    {
    }
    #endregion [ Child Data Access ]

    protected void FetchRole(Role data, RoleLoadSettings loadSettings)
    {
      FetchRoleData(data, loadSettings);
      FetchRoleChildren(data, loadSettings);
    }

    protected void FetchRoleData(Role data, RoleLoadSettings loadSettings)
    {
      this.RoleId = data.RoleId;
      this.Name = data.Name;
      this.IsAdmin = data.IsAdmin;
    }

    protected void FetchRoleChildren(Role data, RoleLoadSettings loadSettings)
    {
      if (loadSettings.Inclusion.IncludeSecurableActions)
        this.SecurableActions = CslaSecurableActionEditCollection.GetSecurableActionEditCollection(data.SecurableActions, loadSettings.Inclusion.IncludedSecurableActionsSettings);
      if (loadSettings.Inclusion.IncludeUsers)
        this.Users = CslaUserEditCollection.GetUserEditCollection(data.Users, loadSettings.Inclusion.IncludedUsersSettings);
    }
    #endregion [ Data Access ]

    #region [ Explicit IRole implementation ]

    Guid IRole.RoleId
    {
      get { return this.RoleId; }
    }

    string IRole.Name
    {
      get { return this.Name; }
      set
      {
        this.Name = value;
      }
    }

    bool IRole.IsAdmin
    {
      get { return this.IsAdmin; }
      set
      {
        this.IsAdmin = value;
      }
    }

    IBusinessObjectCollection<ISecurableAction> IRole.SecurableActions
    { 
      get
      {
        return this.SecurableActions;
      }
    }

    IBusinessObjectCollection<IUser> IRole.Users
    { 
      get
      {
        return this.Users;
      }
    }


    bool IBusinessObject.IsReadonly
    {
      get { return false; }
    }

    bool IBusinessObject.IsDirty
    {
      get { return this.IsDirty; }
    }

    #endregion [ Explicit IRole implementation ]
  }
}
